package docx;


import java.io.FileReader;
import java.io.IOException;


import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;

class html2docx {
	public static void main(String[] args) throws Docx4JException, IOException {
		
		// read a html file
		FileReader fileReader = new FileReader("C:/me2.html");
		String html = "";
		int i;
		while ((i = fileReader.read()) != -1) {
			char ch = (char) i;
			html = html + ch;
		}

		// HTML code


		// create the package
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
				.createPackage();

		AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
				new PartName("/me2.html"));
		afiPart.setBinaryData(html.getBytes());
		afiPart.setContentType(new ContentType("text/html"));
		
		Relationship altChunkRel = wordMLPackage.getMainDocumentPart()
				.addTargetPart(afiPart);

		// .. the bit in document body
		CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
		ac.setId(altChunkRel.getId());
		wordMLPackage.getMainDocumentPart().addObject(ac);

		// .. content type
		wordMLPackage.getContentTypeManager().addDefaultContentType("html",
				"text/html");
		wordMLPackage.save(new java.io.File(System.getProperty("user.dir")
				+ "/sample-docs/test.docx"));

	
	}
}
